package com.qn.example.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qn.example.exception.CustomerException;
import com.qn.example.mapper.CategoryMapper;
import com.qn.example.mapper.DishMapper;
import com.qn.example.mapper.SetmealMapper;
import com.qn.example.pojo.Category;
import com.qn.example.pojo.Page;
import com.qn.example.pojo.R;
import com.qn.example.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author 21276
 * @version 1.0
 * @date 2023/5/4 14:27
 */
@Slf4j
@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired(required = false)
    private CategoryMapper categoryMapper;
    @Autowired(required = false)
    private DishMapper dishMapper;
    @Autowired(required = false)
    private SetmealMapper setmealMapper;

    @Override
    public Page<Category> findByPage(Integer page, Integer pageSize) {
        //开启分页
        PageHelper.startPage(page,pageSize);

        //调用dao层的方法查询数据
        List<Category> pageList = categoryMapper.findByPage(null);
        //封装封装PageInfo对象
        PageInfo<Category> pageInfo = new PageInfo<>(pageList);
        //封装返回给页面的Page对象
        Page<Category> pageBean = new Page<>();
        //向page对象中添加数据
        pageBean.setRecords(pageInfo.getList());
        pageBean.setPage(pageInfo.getPageNum());
        pageBean.setTotal(pageInfo.getTotal());
        pageBean.setPageSize(pageInfo.getPageSize());

        return pageBean;
    }

    @Override
    public void save(Category category) {
        //设置创建时间和修改时间
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        //调用dao层添加数据的方法
        categoryMapper.save(category);
    }

    @Override
    public void updateEmp(Category category) {
        //更新记录的修改时间！
        category.setUpdateTime(LocalDateTime.now());
        System.out.println("修改的时间："+category.getUpdateTime());
        categoryMapper.updateEmp(category);
    }

    //删除
    @Override
    public void delete(Long id) {
        /**
         * 判断当前分类有没有关联菜品
         * 当前分类有没有关联套餐
         */
        //判断当前菜系的id有没有关联菜品
        Integer dishNum = dishMapper.findDishCountByCategoryId(id);
        if (dishNum>0){
            //代表当前菜系和菜品还有关联
            throw new CustomerException(R.error("该类别已经关联菜品不能直接删除"));
        }
        Integer setmealNum = setmealMapper.findDishCountByCategoryId(id);
        if (setmealNum>0){
            //代表当前菜系和菜品还有关联
            throw new CustomerException( R.error("该类别已经关联套餐不能直接删除"));
        }
        categoryMapper.deleteId(id);
    }

    @Override
    public List<Category> foodClassNames(Integer type) {
       return categoryMapper.foodClassNames(type);
    }

    @Override
    public List<Category> findAllCategory() {
        return categoryMapper.findByPage(null);
    }
}
